package com.zrc.superframe_base.mvx.base

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.components.SimpleImmersionOwner
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions
import com.lxj.xpopup.impl.LoadingPopupView
import com.zrc.superframe_aop.annotation.SingleClick
import me.jessyan.autosize.internal.CustomAdapt
import me.yokeyword.fragmentation.*
import me.yokeyword.fragmentation.anim.FragmentAnimator

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
abstract class BaseActivity : AppCompatActivity(), ISupportActivity, SimpleImmersionOwner, CustomAdapt, View.OnClickListener {

    lateinit var mDelegate : SupportActivityDelegate

    lateinit var xxPermissions: XXPermissions

    lateinit var loadingPopupView: LoadingPopupView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDelegate = SupportActivityDelegate(this)
        ARouter.getInstance().inject(this)
        mDelegate.onCreate(savedInstanceState)
        xxPermissions = XXPermissions.with(this)
        if (immersionBarEnabled()) initImmersionBar()
        setPortraitScreen()
    }

    @SuppressLint("CheckResult")
    fun requestPermission(vararg permissions: String, success: (() -> Unit)? = null, failed: (() -> Unit)? = null) {
        xxPermissions.permission(permissions).request(object : OnPermissionCallback {
            override fun onGranted(permissions: MutableList<String>, all: Boolean) {
                if (all) {//所有权限全部通过
                    success?.let { it() }
                } else {//有部分权限被拒绝（包括被永久拒绝的权限）
                    // 获取没有授予的权限
                    val p0=XXPermissions.getDenied( this@BaseActivity, permissions)
                    if(p0.isNullOrEmpty()){//如果p0为空说明全部都通过了
                        onGranted(permissions.toList().toMutableList(),true)
                    }else{//如果p0不为空
                        // 判断一个或多个权限是否被永久拒绝了
                        val p= XXPermissions.isPermanentDenied(this@BaseActivity,permissions);
                        onDenied(p0, p)
                    }
                }
            }

            override fun onDenied(permissions: MutableList<String>, never: Boolean) {
                if (never) {
                    XXPermissions.startPermissionActivity(this@BaseActivity, permissions)
                    failed?.let { it() }
                } else {
                    failed?.let { it() }
                }
            }
        })
    }

    override fun getSupportDelegate(): SupportActivityDelegate? = mDelegate

    override fun extraTransaction(): ExtraTransaction = mDelegate.extraTransaction()

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        mDelegate.onPostCreate(savedInstanceState)
    }

    override fun onDestroy() {
        mDelegate.onDestroy()
        super.onDestroy()
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean = mDelegate.dispatchTouchEvent(ev) || super.dispatchTouchEvent(ev)

    override fun onBackPressed() {
        mDelegate.onBackPressed()
    }

    override fun onBackPressedSupport() {
        mDelegate.onBackPressedSupport()
    }

    override fun getFragmentAnimator(): FragmentAnimator? = mDelegate.fragmentAnimator

    override fun setFragmentAnimator(fragmentAnimator: FragmentAnimator?) {
        mDelegate.fragmentAnimator = fragmentAnimator
    }

    override fun onCreateFragmentAnimator(): FragmentAnimator? {
        return mDelegate.onCreateFragmentAnimator()
    }

    override fun post(runnable: Runnable?) {
        mDelegate.post(runnable)
    }

    override fun immersionBarEnabled() = false

    override fun initImmersionBar() {
        //初始化沉浸式
    }

    override fun isBaseOnWidth(): Boolean {
        return false//true为按照宽度适配, false为按照高度适配
    }

    override fun getSizeInDp() = 667.toFloat()//isBaseOnWidth【true为总宽度，false为总高度】

    open fun setPortraitScreen(isPortrait:Boolean=true) {//设置屏幕方向 默认竖屏
        requestedOrientation = if (isPortrait) {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }

    @SingleClick
    override fun onClick(view: View) {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        supportFragmentManager.fragments.forEach {
            it.onActivityResult(requestCode, resultCode, data)
        }
    }
}
